package com.lxmes.device.controller;

import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.core.page.TableDataInfo;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.device.domain.NcEmJdgz;
import com.lxmes.device.service.INcEmJdgzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 设备点检规则Controller
 *
 * @author ruoyi
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/device/jdgz")
public class NcEmJdgzController extends BaseController
{
    @Autowired
    private INcEmJdgzService ncEmJdgzService;

    /**
     * 查询设备点检规则列表
     */
    @PreAuthorize("@ss.hasPermi('device:jdgz:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcEmJdgz ncEmJdgz)
    {
        startPage();
        List<NcEmJdgz> list = ncEmJdgzService.selectNcEmJdgzList(ncEmJdgz);
        return getDataTable(list);
    }

    /**
     * 导出设备点检规则列表
     */
    @PreAuthorize("@ss.hasPermi('device:jdgz:export')")
    @Log(title = "设备点检规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcEmJdgz ncEmJdgz)
    {
        List<NcEmJdgz> list = ncEmJdgzService.selectNcEmJdgzList(ncEmJdgz);
        ExcelUtil<NcEmJdgz> util = new ExcelUtil<NcEmJdgz>(NcEmJdgz.class);
        util.exportExcel(response, list, "设备点检规则数据");
    }

    /**
     * 获取设备点检规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:jdgz:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncEmJdgzService.selectNcEmJdgzById(id));
    }

    /**
     * 新增设备点检规则
     */
    @PreAuthorize("@ss.hasPermi('device:jdgz:add')")
    @Log(title = "设备点检规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcEmJdgz ncEmJdgz)
    {
        return toAjax(ncEmJdgzService.insertNcEmJdgz(ncEmJdgz));
    }

    /**
     * 修改设备点检规则
     */
    @PreAuthorize("@ss.hasPermi('device:jdgz:edit')")
    @Log(title = "设备点检规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcEmJdgz ncEmJdgz)
    {
        return toAjax(ncEmJdgzService.updateNcEmJdgz(ncEmJdgz));
    }

    /**
     * 删除设备点检规则
     */
    @PreAuthorize("@ss.hasPermi('device:jdgz:remove')")
    @Log(title = "设备点检规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncEmJdgzService.deleteNcEmJdgzByIds(ids));
    }

    /**
     * 导入设备点检规则
     */
    @Log(title = "设备点检规则", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcEmJdgz> util = new ExcelUtil<>(NcEmJdgz.class);
        List<NcEmJdgz> ncEmJdgzList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncEmJdgzService.imporIteminfo(ncEmJdgzList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcEmJdgz> util = new ExcelUtil<>(NcEmJdgz.class);
        util.importTemplateExcel(response,"设备点检规则");
    }
}
